<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form :model="formData" :rules="rules" label-width="120px" style="margin-left: 120px; margin-top:30px">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="formData.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="newpassword">
                <el-input v-model="formData.newpassword" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="submit">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <!-- <userInfo /> -->
            <!-- 动态组件使用方法 -->
            <component :is="userInfoName" />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <!-- 组件第一种用法 -->
            <!-- <jonInfo /> -->
            <!-- 第二种用法：动态组件使用方法 -->
            <component :is="jobInfoName" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
import userInfo from './components/user-info.vue'
import jonInfo from './components/jon-info.vue'

export default {
  components: {
    userInfo, jonInfo
  },
  data() {
    return {
      userInfoName: 'userInfo',
      jobInfoName: 'jonInfo',
      userId: this.$route.params.id,
      formData: {
        username: '',
        newpassword: ''
      },
      rules: {
        username: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ],
        newpassword: [
          { required: true, message: '该项不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    //   数据回显，显示账号到输入框中
    async getUserInfo() {
      this.formData = await getUserDetailById(this.userId)
    },
    // 点击更新提交新的密码
    async submit() {
      // 发请求
      await saveUserDetailById({ ...this.formData, password: this.formData.newpassword })
      //   提示用户
      this.$message.success('更新成功')
      //   清空密码框
      this.formData.newpassword = ''
    }
  }
}
</script>

<style>

</style>
